﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class Exercise1 : MonoBehaviour
{
    
    void Start()
    {
        //XmlDocument xml = new XmlDocument();
        //xml.Load(Application.streamingAssetsPath + "/te.xml");

        //XmlNode root = xml.SelectSingleNode("PlayerInfo");
        //XmlNode nameNode = root.SelectSingleNode("name");
        //print(nameNode.InnerText);

        //XmlNode atkNode = root.SelectSingleNode("atk");
        //print(atkNode.InnerText);

        //XmlNode defNode = root.SelectSingleNode("def");
        //print(defNode.InnerText);

        //XmlNode weaponNode = root.SelectSingleNode("weapon");
        //print(weaponNode.Attributes["id"].Value);
        //print(weaponNode.Attributes["num"].Value);

        //XmlNodeList nodeList = root.SelectNodes("itemDic");
        //foreach (XmlNode node in nodeList)
        //{
        //    print(node.SelectSingleNode("int").InnerText);
        //    print(node.SelectSingleNode("Item").Attributes["id"].Value);
        //    print(node.SelectSingleNode("Item").Attributes["num"].Value);
        //}

        //保存数据到xml中
        string path = Application.persistentDataPath + "/t.xml";
        print(Application.persistentDataPath);
        PlayerInfo playerInfo = new PlayerInfo();

        XmlDocument xml = new XmlDocument();
        //声明xml版本信息和编码格式
        XmlDeclaration xmlde = xml.CreateXmlDeclaration("1.0", "UTF-8", "");
        //添加头文件
        xml.AppendChild(xmlde);

        //创建根节点
        XmlElement root = xml.CreateElement("Root");
        xml.AppendChild(root);
        //创建子节点
        AddElement(xml, root, "name", playerInfo.name);
        AddElement(xml, root, "atk", playerInfo.atk);
        AddElement(xml, root, "def", playerInfo.def);
        //XmlElement nameElement = xml.CreateElement("name");
        //nameElement.InnerText = playerInfo.name;
        //root.AppendChild(nameElement);

        //添加属性
        AddElement(xml, root, "Item", playerInfo.item);
        //XmlElement xmlItem = xml.CreateElement("Item");
        //xmlItem.SetAttribute("id", playerInfo.item.id.ToString());
        //xmlItem.SetAttribute("num", playerInfo.item.num.ToString());
        //root.AppendChild(xmlItem);

        //添加普通列表
        AddElement(xml, root, "intList", playerInfo.intList);

        AddElement(xml, root, "itemList", playerInfo.itemList);

        AddElement(xml, root, "itemDic", playerInfo.dic);

        xml.Save(path);

        //判断文件是否存在
        if (File.Exists(path))
        {
            //修改节点
            xml = new XmlDocument();
            xml.Load(path);
            XmlNode xroot = xml.SelectSingleNode("Root");
            XmlNode namenode = xroot.SelectSingleNode("name");
            namenode.InnerText = "金色闪光";

            //添加
            XmlElement newAdd = xml.CreateElement("newAdd");
            newAdd.InnerText = "凉瓜";
            xroot.AppendChild(newAdd);

            //删除
            //XmlNode def = xroot.SelectSingleNode("def");
            XmlNode def = xml.SelectSingleNode("Root/def");
            xroot.RemoveChild(def);

            xml.Save(path);
        }

    }

    public void AddElement(XmlDocument xml,XmlElement root, string nodeName,object obj)
    {
        Type type = obj.GetType();
        XmlElement nodeElement = xml.CreateElement(nodeName);
        if (type == typeof(string))
        {
            nodeElement.InnerText = obj as string;
            root.AppendChild(nodeElement);
        }
        else if (type == typeof(int))
        {
            nodeElement.InnerText = obj.ToString();
            root.AppendChild(nodeElement);
        }
        else if (typeof(IList).IsAssignableFrom(type))
        {
            IList list = obj as IList;
            for (int i = 0; i < list.Count; i++)
            {
                //XmlElement node = xml.CreateElement("int"); 
                //node.InnerText = list[i].ToString();
                //nodeElement.AppendChild(node);
                AddElement(xml, nodeElement, "element", list[i]);
            }
            root.AppendChild(nodeElement);
        }
        else if (typeof(IDictionary).IsAssignableFrom(type))
        {
            IDictionary dic = obj as IDictionary;
            foreach (object key in dic.Keys)
            {
                AddElement(xml, nodeElement, "key", key);
                AddElement(xml, nodeElement, "item", dic[key]);
            }
            root.AppendChild(nodeElement);
        }
        else
        {
            Item item = obj as Item;
            nodeElement.SetAttribute("id", item.id.ToString());
            nodeElement.SetAttribute("num", item.num.ToString());
            root.AppendChild(nodeElement);
        }
    }
}
